// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-09-03 14:00:02
 * @LastEditors: wxc superjava@qq.com
 * @LastEditTime: 2024-09-29 12:56:26
 * @FilePath: /SGFEM/DBManager/src/Service/Property/PBRSECTService.cpp
 */
#include "DBManager/Service/Input/Property/PBRSECTService.h"

#include <cassert>

#include "DBManager/Common/ServiceMacro.h"
#include "DBManager/IRepository/Input/Property/IPBRSECTRepository.h"
#include "DBManager/IService/Input/Node/IPointService.h"
#include "DBManager/IService/Input/Partition/ISET1Service.h"
#include "DBManager/Service/Input/Property/PropertySummaryService.h"

using namespace SG::DBManager;

SERVICE_REG_WITH_DBM_IMPL (PBRSECTService, IPBRSECTRepository)

SERVICE_REG_SUMMARY (PBRSECTService, PropertySummaryService)
PBRSECTService::PBRSECTService (DBServiceFactory* const dbServiceFactory, IPBRSECTRepository& repos)
    : m_repository (repos), m_dbServiceFactory (dbServiceFactory)
{}

PBRSECTService::~PBRSECTService () = default;

SG::DataStructure::Common::Status
GetSET1PointData (DBServiceFactory* m_dbServiceFactory, SG::DataStructure::FEM::SET1Data sd, std::vector<SG::Algebra::Point>& points)
{
    SG::DataStructure::Common::Id                     upGid;
    std::unordered_set<SG::DataStructure::Common::Id> idSet;
    for (auto gid : sd.m_gids)
    {
        // 存在负数，需要补全
        if (gid < 0)
        {
            SG::DataStructure::Common::Id curGid = -gid;
            for (SG::DataStructure::Common::Id i = upGid + 1; i <= curGid; i++)
            {
                if (idSet.find (i) == idSet.end ())
                    idSet.insert (i);
                else
                    continue;
                SG::DataStructure::FEM::PointData pt;
                if (m_dbServiceFactory->get<SG::DBManager::IPointService> ()->FindById (i, pt)
                    == SG::DataStructure::Common::Status::STATUS_Failure)
                {
                    SGError << "Failed to obtain node data";
                    return SG::DataStructure::Common::Status::STATUS_Failure;
                }
                points.push_back ({ 0, pt.m_X[0], pt.m_X[1] });
            }
            continue;
        }

        if (idSet.find (gid) == idSet.end ())
            idSet.insert (gid);
        else
            continue;

        SG::DataStructure::FEM::PointData pt;
        if (m_dbServiceFactory->get<SG::DBManager::IPointService> ()->FindById (gid, pt)
            == SG::DataStructure::Common::Status::STATUS_Failure)
        {
            SGError << "Failed to obtain node data";
            return SG::DataStructure::Common::Status::STATUS_Failure;
        }
        points.push_back ({ 0, pt.m_X[0], pt.m_X[1] });

        upGid = gid;
    }

    return SG::DataStructure::Common::Status::STATUS_Success;
}

SG::DataStructure::Common::Status PBRSECTService::ConvertToBeamSectionArbitrary (SG::DataStructure::Common::Id                 id,
                                                                                 SG::DataStructure::FEM::BeamSectionArbitrary& beamSection)
{
    SG::DataStructure::FEM::PBRSECTData pbrsect;
    if (FindById (id, pbrsect) != SG::DataStructure::Common::Status::STATUS_Success)
    {
        return SG::DataStructure::Common::Status::STATUS_Failure;
    }
    beamSection.m_id = pbrsect.m_id;

    // 外轮廓
    SG::DataStructure::FEM::SET1Data sd;
    if (m_dbServiceFactory->get<ISET1Service> ()->FindById (pbrsect.m_OUTP, sd) == SG::DataStructure::Common::Status::STATUS_Failure)
    {
        SGError << "Failed to obtain SET data";
        return SG::DataStructure::Common::Status::STATUS_Failure;
    }

    GetSET1PointData (m_dbServiceFactory, sd, beamSection.m_outer);

    for (auto holeSet : pbrsect.m_sections)
    {
        for (auto holeSet : holeSet.inps)
        {
            SG::DataStructure::FEM::SET1Data hSet;
            if (m_dbServiceFactory->get<ISET1Service> ()->FindById (holeSet.ID, hSet) == SG::DataStructure::Common::Status::STATUS_Failure)
            {
                SGError << "Failed to obtain SET data";
                return SG::DataStructure::Common::Status::STATUS_Failure;
            }

            std::vector<SG::Algebra::Point> hole;
            GetSET1PointData (m_dbServiceFactory, hSet, hole);
            beamSection.m_holes.emplace_back (hole);
        }
    }

    return SG::DataStructure::Common::Status::STATUS_Success;
}